package com.ball.dev.controller;


import com.ball.common.dtos.ResponseResult;
import com.ball.common.enums.AppHttpCodeEnum;
import com.ball.common.utils.BeanUtils;
import com.ball.dev.entity.po.Jersey;
import com.ball.dev.entity.po.PostComment;
import com.ball.dev.entity.vo.JerseyVo;
import com.ball.dev.entity.vo.JerseyVo1;
import com.ball.dev.service.IJerseyService;
import com.ball.dev.service.IPostCommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 球衣模版表
 *
 * @author lou
 * @since 2024-04-10
 */
@RestController
@RequestMapping("/jersey")
@RequiredArgsConstructor
public class JerseyController {

    private  final IJerseyService jerseyService;
    private final IPostCommentService postCommentService;
    @PostMapping("/addJersey")
    public ResponseResult addJersey(@RequestBody Jersey jersey) {
        return jerseyService.addJersey(jersey);
    }
    @GetMapping("/getJerseyList")
    public ResponseResult getJerseyList() {
        return jerseyService.getJerseyList();
    }


    /**
     * 根据ID获取Jersey信息及其评论。
     *
     * @param id 表示Jersey的唯一标识符。
     * @return 返回一个ResponseResult对象，如果成功找到Jersey并附带评论信息，则返回状态OK和相关信息；如果找不到Jersey，则返回数据不存在的错误信息。
     */
    @GetMapping("/{id}")
    public ResponseResult getJerseyById(@PathVariable String id) {
        // 通过ID从jerseyService获取Jersey对象
        Jersey jersey = jerseyService.getById(id);

        // 检查jersey是否存在，如果不存在返回错误信息
        if (jersey == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        // 查询并排序该Jersey下的所有评论
        List<PostComment> comments = postCommentService.lambdaQuery().eq(PostComment::getPostId, jersey.getId())
                .orderByDesc(PostComment::getCreateTime).list();

        // 将Jersey对象复制到JerseyVo1对象，并设置评论信息
        JerseyVo1 jerseyVo1 = BeanUtils.copyBean(jersey, JerseyVo1.class);
        jerseyVo1.setPostComments(comments);

        // 返回成功结果，包含处理后的JerseyVo1对象
        return ResponseResult.okResult(jerseyVo1);
    }
}
